package graphicalUserInterface;

import java.io.IOException;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.xml.sax.SAXException;

import email.EmailManager;

//@author U096819H
public class EmailView extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text txtToEmail;
	private Text txtSubject;
	private Text txtMessage;

	// @author U096819H
	public EmailView(Shell parent, int style) {
		super(parent, style);
		setText("Email Expense");
	}

	// @author U096819H
	public Object open(String message, String type) {
		createContents(message, type);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	// @author U096819H
	private void createContents(final String message, final String type) {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 233);
		shell.setText(getText());
		shell.setLayout(new FormLayout());

		final Composite formHolder = new Composite(shell, SWT.NONE);
		FormData fd_formHolder = new FormData();
		fd_formHolder.top = new FormAttachment(0, 10);
		fd_formHolder.left = new FormAttachment(0, 10);
		fd_formHolder.bottom = new FormAttachment(0, 200);
		fd_formHolder.right = new FormAttachment(0, 434);
		formHolder.setLayoutData(fd_formHolder);
		formHolder.setLayout(new GridLayout(2, false));

		Label lblTo = new Label(formHolder, SWT.NONE);
		lblTo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblTo.setText("To");

		txtToEmail = new Text(formHolder, SWT.BORDER);
		txtToEmail.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label lblSubject = new Label(formHolder, SWT.NONE);
		lblSubject.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblSubject.setText("Subject");

		txtSubject = new Text(formHolder, SWT.BORDER);
		txtSubject.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label lblMessage = new Label(formHolder, SWT.NONE);
		lblMessage.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblMessage.setText("Message");

		txtMessage = new Text(formHolder, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_txtMessage = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_txtMessage.heightHint = 92;
		txtMessage.setLayoutData(gd_txtMessage);
		new Label(formHolder, SWT.NONE);

		Button btnSend = new Button(formHolder, SWT.NONE);
		btnSend.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				try {
					EmailManager em = new EmailManager();
					String msg = txtMessage.getText() + "\n\n---- " + type
							+ " Details ----\n" + message + "\n\n";
					em.send(txtToEmail.getText(), txtSubject.getText(), msg);
					NotifyPrompt ntp = new NotifyPrompt(formHolder.getShell(),
							65);
					ntp.open("Mail Sent to \n" + txtToEmail.getText());
					shell.close();
				} catch (Exception e1) {
					NotifyPrompt ntp = new NotifyPrompt(formHolder.getShell(),
							65);
					ntp.open("Failed to send mail to \n" + txtToEmail.getText());
					shell.close();
				}
			}
		});
		GridData gd_btnSend = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_btnSend.widthHint = 69;
		btnSend.setLayoutData(gd_btnSend);
		btnSend.setText("Send");
	}
}
